using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The SchoolMajorTableDB class is responsible for interacting with the database to retrieve and store information
    /// about SchoolMajorTable objects.
    /// </summary>
    public class SchoolMajorTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of SchoolMajorTable from the underlying datasource.
        /// </summary>
        /// <param name="schoolMajorKey">The unique SchoolMajorKey of the SchoolMajorTable in the database.</param>
        /// <returns>An SchoolMajorTable when the SchoolMajorKey was found in the database, or null otherwise.</returns>
        public static SchoolMajorTable GetItem(int schoolMajorKey)
        {
            SchoolMajorTable mySchoolMajorTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolMajorTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolMajorKey", schoolMajorKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            mySchoolMajorTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return mySchoolMajorTable;
            }
        }

        /// <summary>
        /// Returns a list with SchoolMajorTable objects.
        /// </summary>
        /// <returns>A generics List with the SchoolMajorTable objects.</returns>
        public static SchoolMajorTableList GetList(int SchoolKey)
        {
            SchoolMajorTableList tempList = new SchoolMajorTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolMajorTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a SchoolMajorTable in the database.
        /// </summary>
        /// <param name="mySchoolMajorTable">The SchoolMajorTable instance to save.</param>
        /// <returns>The new SchoolMajorKey if the SchoolMajorTable is new in the database or the existing SchoolMajorKey when an item was updated.</returns>
        public static int Save(SchoolMajorTable mySchoolMajorTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolMajorTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (mySchoolMajorTable.SchoolMajorKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorKey", mySchoolMajorTable.SchoolMajorKey);
                    }
                    if (mySchoolMajorTable.SchoolKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", mySchoolMajorTable.SchoolKey);
                    }
                    if (string.IsNullOrEmpty(mySchoolMajorTable.SchoolMajorCode))
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolMajorCode", mySchoolMajorTable.SchoolMajorCode);
                    }
                    if (string.IsNullOrEmpty(mySchoolMajorTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", mySchoolMajorTable.Description);
                    }
                    if (mySchoolMajorTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", mySchoolMajorTable.Status);
                    }
                    //if (mySchoolMajorTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", mySchoolMajorTable.CreatedDateTime);
                    //}
                    //if (mySchoolMajorTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", mySchoolMajorTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a SchoolMajorTable from the database.
        /// </summary>
        /// <param name="schoolMajorKey">The SchoolMajorKey of the SchoolMajorTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int schoolMajorKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolMajorTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@schoolMajorKey", schoolMajorKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the SchoolMajorTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static SchoolMajorTable FillDataRecord(IDataRecord myDataRecord)
        {
            SchoolMajorTable mySchoolMajorTable = new SchoolMajorTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolMajorKey")))
            {
                mySchoolMajorTable.SchoolMajorKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolMajorKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                mySchoolMajorTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolMajorCode")))
            {
                mySchoolMajorTable.SchoolMajorCode = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolMajorCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                mySchoolMajorTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                mySchoolMajorTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                mySchoolMajorTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                mySchoolMajorTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return mySchoolMajorTable;
        }
    }

}
